//
//  XYTitleButton.swift
//  SinaWeibo
//
//  Created by 韩啸宇 on 15/12/23.
//  Copyright © 2015年 backy. All rights reserved.
//

import UIKit

class XYTitleButton: UIButton {
    
    convenience init(title: String?, imageName: String?, selectedImageName: String?, target: AnyObject?, action: Selector) {
        self.init()
        
        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        if let imgName = imageName {
            setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        }
        
        if let selectedImgName = selectedImageName {
            setImage(UIImage(named: selectedImgName), forState: UIControlState.Selected)
        }
        
        if let tmpTarget = target {
            addTarget(tmpTarget, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        sizeToFit()
    }
    

    override func layoutSubviews() {
        super.layoutSubviews()
        
        titleLabel?.frame.origin.x = -5
        imageView?.frame.origin.x = (titleLabel?.frame.size.width)! + 5
    }
}
